package com.atguigu.controller;

import com.atguigu.controller.base.ModelBaseServlet;
import com.atguigu.pojo.Book;
import com.atguigu.pojo.PageInfo;
import com.atguigu.pojo.ResultVO;
import com.atguigu.service.BookService;
import com.atguigu.service.impl.BookServiceImpl;
import com.atguigu.utils.JSONUtils;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.bind.util.JAXBSource;
import java.awt.image.RescaleOp;
import java.io.IOException;
import java.util.List;

/**
 * @Author luoyin
 * @Date 18:06 2022/7/18
 * @Description 图书管理
 **/

@WebServlet("/book")
public class BookController extends ModelBaseServlet {

    /*
     * 分页展示并跳转到book_manager页面
     * @param: request
     * @Param: response
     * @return:void
     **/
    public void toBookManager(HttpServletRequest request , HttpServletResponse response){
        try {
            String currentPageStr = request.getParameter("currentPage");
            if(currentPageStr == null || "".equals(currentPageStr)){
                currentPageStr = "1";
            }
            int currentPage = Integer.parseInt(currentPageStr);
            BookService bookService = new BookServiceImpl();
            PageInfo<Book> pageInfo = bookService.selectByPage(currentPage);
            request.setAttribute("pageInfo",pageInfo);
            processTemplate("/pages/manager/book_manager",request,response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //添加,跳转到添加页面
    public void toBookAdd(HttpServletRequest request , HttpServletResponse response){
        try {
            processTemplate("/pages/manager/book_add",request,response);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //修改,跳转到修改页面
    public void toBookUpdate(HttpServletRequest request , HttpServletResponse response){
        try {
            String bookIdStr = request.getParameter("bookId");
            int bookId = Integer.parseInt(bookIdStr);
            BookService bookService = new BookServiceImpl();
            Book book = bookService.selectById(bookId);
            request.setAttribute("book",book);
            processTemplate("/pages/manager/book_edit",request,response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //删除,跳转到删除页面
    public void delete(HttpServletRequest request , HttpServletResponse response) throws Exception {
        String bookIdStr = request.getParameter("bookId");
        int bookId = Integer.parseInt(bookIdStr);
        BookService bookService = new BookServiceImpl();
        bookService.delete(bookId);
        response.sendRedirect(request.getContextPath()+"/book?method=toBookManager");
    }
    //添加
    public void add(HttpServletRequest request , HttpServletResponse response){
        Book book = new Book();
        try {
            BeanUtils.populate(book,request.getParameterMap());
            BookService bookService = new BookServiceImpl();
            //System.out.println(1/0);
            int add = bookService.add(book);
            if(add >0){
                //添加成功,重定向到图书管理
                response.sendRedirect(request.getContextPath()+"/book?method=toBookManager");
            }else {
                //添加失败,还在该页面
                request.setAttribute("book",book);
                toBookAdd(request, response);
            }

        } catch (Exception e) {
            request.setAttribute("book",book);
            toBookAdd(request, response);
            e.printStackTrace();
        }
    }

    //修改
    public void update(HttpServletRequest request , HttpServletResponse response){
        Book book = new Book();
        try {
            BeanUtils.populate(book,request.getParameterMap());
            BookService bookService = new BookServiceImpl();
            int update = bookService.update(book);
            if(update > 0){
                //修改成功,重定向到图书管理页面
                response.sendRedirect(request.getContextPath()+"/book?method=toBookManager");
            }else {
                //修改失败
                request.setAttribute("book",book);
                processTemplate("/pages/manager/book_edit",request,response);
            }
        } catch (Exception e) {
            e.printStackTrace();
            try {
                request.setAttribute("book",book);
                processTemplate("/pages/manager/book_edit",request,response);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }

    //查询xx到xx价格区间的图书信息,并分页
    public void selectList(HttpServletRequest request , HttpServletResponse response){

        try {
            Double floorPrice = Double.valueOf(request.getParameter("floorPrice"));
            Double peakPrice = Double.valueOf(request.getParameter("peakPrice"));

            String currentPageStr = request.getParameter("currentPage");
            if(currentPageStr == null || "".equals(currentPageStr)){
                currentPageStr = "1";
            }
            int currentPage = Integer.parseInt(currentPageStr);
            BookService bookService = new BookServiceImpl();
            PageInfo<Book> pageInfo = bookService.selectList(currentPage,floorPrice,peakPrice);
            ResultVO<PageInfo> resultVO = new ResultVO<>(true,"查询价格列表成功",pageInfo);
            JSONUtils.object2JsonStrAndresponse(response,resultVO);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //首页分页查询
    public void selectByPage(HttpServletRequest request , HttpServletResponse response){
        BookService bookService = new BookServiceImpl();

        String currentPage =request.getParameter("currentPage");
        if (currentPage == null){
            currentPage="1";
        }
        PageInfo<Book> pageInfo = null;
        ResultVO<PageInfo> resultVO = null;
        //List<Book> bookList= null;
        //ResultVO<List> resultVO = null;

        try {
            //bookList = bookService.selectAll();
            //resultVO = new ResultVO<>(true,"查询图书列表成功",bookList);

            pageInfo = bookService.selectByPage(Integer.parseInt(currentPage));
            resultVO = new ResultVO<>(true,"查询图书列表成功",pageInfo);

        } catch (Exception e) {
            e.printStackTrace();
            //resultVO = new ResultVO<>(false,"查询图书列表失败",bookList);
            resultVO = new ResultVO<>(false,"查询图书列表失败",pageInfo);
        }
        try {
            JSONUtils.object2JsonStrAndresponse(response,resultVO);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
